package com.weme.live.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

class DragView(context: Context, attrs: AttributeSet? = null)  : View(context, attrs) {
    private var offsetX = 0
    private var offsetY = 0

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                offsetX = event.x.toInt()
                offsetY = event.y.toInt()
            }
            MotionEvent.ACTION_MOVE -> {
                val newX = event.rawX.toInt() - offsetX
                val newY = event.rawY.toInt() - offsetY

                // 获取屏幕的宽度和高度
                val screenWidth = resources.displayMetrics.widthPixels
                val screenHeight = resources.displayMetrics.heightPixels

                // 计算 view 的右下角坐标
                val right = newX + width
                val bottom = newY + height

                // 限制 view 移动范围，确保不会超出屏幕
                if (newX >= 0 && right <= screenWidth && newY >= 0 && bottom <= screenHeight - 200) { // 更新拖动的 view 的位置
                    x = newX.toFloat()
                    y = newY.toFloat()
                }
            }
        }
       return true
    }

    }